// merchant//merchants/receiveJJk/index.js
const app = getApp();
const utils = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {}
  },

  receiveJJK: function (e) {
    let that = this;
    utils.getMobile(e, function (mobiles) {
      if (mobiles) {
        that.receiveJJKConfirm();
      } else {
        wx.showToast({
          title: '请授权手机号码',
          mask: true
        })
      }
    });
  },

  receiveJJKConfirm: function () {
    let that = this;
    let info = that.data.info;
    let postData = {
      uid: app.globalData.userInfo.Uid,
      saleUid: info.createUid,
      cardItemId: info.cardItemId,
      code: parseInt(info.timestamp),
      source: info.source
    }
    console.log('领取玖玖卡参数', postData);
    wx.request({
      url: app.globalData.host + '/api/Coupon/ReceiveCard?uid=' + app.globalData.userInfo.Uid + '&saleUid=' + info.createUid + '&cardItemId=' + info.cardItemId + '&code=' + parseInt(info.timestamp) + '&source=' + info.source,
      success: function (res) {
        console.log('转赠玖玖卡领取结果', res);
        if (res.data.Code == 0) {
          wx.showToast({
            title: '领取成功',
            mask: true
          })
          wx.reLaunch({
            url: '/merchant/merchants/myCardCoupon/index?tagindex=5&showWxcard=1',
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('领取玖玖卡页面', options);
    let that = this;
    let params = decodeURIComponent(options.scene);
    if (params && params != 'undefined') {//扫描小程序二维码进入
      let plist = '';
      if (params.indexOf('p-') != -1) {
        let param = params.match(/p-(\S*)/)[1];//参数
        plist = param.split('_');
      } else {
        let param = params.match(/p=(\S*)/)[1];//参数
        plist = param.split('#');
      }
      let info = {};
      info.createUid = plist[2];
      info.cardItemId = plist[1];
      info.timestamp = plist[3];
      info.source = plist[4];
      that.data.info = info;
    } else {
      that.data.info = options;
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }

})